﻿using System;
using System.Collections.Generic;
using System.Text;
using SharePoint2007Migration;

using System.Configuration;
using SharePoint2007Migration.Domain.Collections;
using SharePoint2007Migration.Domain;
using System.IO;

namespace SharePoint2007Migration.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                if (CheckIsParamtersAreValid(args))
                {
                    SaveReportToFile(args[1], CreateReport(args[0]));
                    System.Console.WriteLine(String.Concat("Report generated: ", args[1]));
                }
                else System.Console.WriteLine("Wrong parameters. Use [Executable.exe SharePointDbAttachLogFileToParse.txt reportFile.txt]");

            }
            catch (Exception ex)
            {
                DisplayAndLogExceptionsMessage(ex);
            }

            System.Console.WriteLine("Press any key to quit...");
            System.Console.ReadLine();

        }

        private static void SaveReportToFile(string path, string report)
        {
            using (StreamWriter outfile = new StreamWriter(path))
            {
                outfile.Write(report);
            }
        }

        private static bool CheckIsParamtersAreValid(string[] args)
        {
            if (args.Length == 2)
            {
                FileInfo inputTxtFile = new FileInfo(args[0]);
                if (inputTxtFile.Exists)
                    return true;
            }
            return false;
        }

        private static string CreateReport(string pathToDbAttachLogFile)
        {
            LogHelper.TraceDebugImportant("Report building has started");

            StringBuilder result = new StringBuilder();
            CreateBanner(result);

            LogHelper.TraceDebugImportant("Parsing input file");
            IssueCollection issues = IssuesManager.LoadIssues(pathToDbAttachLogFile);
            LogHelper.TraceDebugImportant(String.Concat(issues.Count, " issues have been found"));

            // feature Report
            var featureIssues = issues.GetAllFeatureIssues();
            LogHelper.TraceDebugImportant(String.Concat(featureIssues.Count, " Feature issues have been found"));

            var webPArtIssues = issues.GetAllWebPartIssues();
            LogHelper.TraceDebugImportant(String.Concat(webPArtIssues.Count, " WebPart issues have been found"));
           
            // ---- add more issues--- 

            LogHelper.TraceDebugImportant("Feature report started");
            MissingFeatureReport featureReport = new MissingFeatureReport(featureIssues);
            result.AppendLine(featureReport.CreateReport());
            LogHelper.TraceDebugImportant("Feature report ended");

            // wp Report
            LogHelper.TraceDebugImportant("WebPars report started");
            MissingWebPartReport webPartReport = new MissingWebPartReport(webPArtIssues);
            
            result.AppendLine(webPartReport.CreateReport());
            LogHelper.TraceDebugImportant("WebPars report ended");

            CreateFooter(result);
            // create Reports.

            LogHelper.TraceDebugImportant("Report building has ended");
            return result.ToString();
        }

        private static void CreateFooter(StringBuilder result)
        {

            result.AppendLine("===============================================");
            result.AppendLine(String.Concat("    Report ended at ", DateTime.Now));
            result.AppendLine("===============================================");
        }

        private static void CreateBanner(StringBuilder result)
        {
            result.AppendLine("===============================================");
            result.AppendLine(String.Concat("    Report started at ", DateTime.Now));
            result.AppendLine("===============================================");
        }

        private static void DisplayAndLogExceptionsMessage(Exception ex)
        {
            System.Console.WriteLine("[Exception]");
            System.Console.WriteLine(String.Concat(ex.Message));
            System.Console.WriteLine("-> StackTrace:");
            System.Console.WriteLine(String.Concat(ex.StackTrace));

            LogHelper.TraceError(ex);
        }
    }
}
